package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parkingspace.entity.Appraise;
import com.parkingspace.dao.AppraiseMapper;
import com.parkingspace.service.IAppraiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.util.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评价表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-06-01
 */
@Service
public class AppraiseServiceImpl extends ServiceImpl<AppraiseMapper, Appraise> implements IAppraiseService {

	@Resource
	private AppraiseMapper appraiseMapper;

	@Override
	public Boolean insertAppraise(String token, Appraise appraise) throws Exception {
		String username = JwtUtils.getUsername(token);
		if (username == null) {
			throw new RuntimeException("请先进行登录!");
		}
		if (appraise == null) {
			throw new RuntimeException("非法参数!");
		}
		boolean b = appraiseMapper.insert(appraise) == 1;
		if (!b) {
			throw new RuntimeException("新增失败!");
		}
		return b;
	}

	@Override
	public List<Appraise> findByOrderId(Long ordereid) throws Exception {
		if (ordereid == null) {
			throw new RuntimeException("非法参数!");
		}
		QueryWrapper<Appraise> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ordereid", ordereid);
		List<Appraise> appraises = appraiseMapper.selectList(queryWrapper);
		if (appraises.size() == 0) {
			throw new RuntimeException("该订单没有评价!");
		}
		return appraises;
	}
}
